package com.uniview.plugins.workplus;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.uniview.entity.vo.OrderMessage;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import poggyio.workplus.po.WorkplusAppMessage;
import poggyio.workplus.po.WorkplusMessageBody;
import poggyio.workplus.po.WorkplusToken;
import poggyio.workplus.service.WorkplusService;

/**
 * 消息推送管理类 所有的消息推送通过这个类来实现
 * 
 * @author Qingtian Lei
 *
 */
@Service
public class WorkplusMessagePushManager {

	static Logger logger = Logger.getLogger(WorkplusMessagePushManager.class);

	@Value("#{configProperties['uniview.grant_type']}")
	private String grant_type; // token类型 (client_credentials | password)

	@Value("#{configProperties['uniview.scope']}")
	private String scope; // 适用类型 (app | user)

	@Value("#{configProperties['uniview.domain_id']}")
	private String domain_id; // 域标识

	@Value("#{configProperties['uniview.org_id']}")
	private String org_id; // 组织标识(组织编号)

	@Value("#{configProperties['uniview.client_id']}")
	private String client_id; // 应用key | 用户名

	@Value("#{configProperties['uniview.client_secret']}")
	private String client_secret; // 应用secret | 用户密码

	@Value("#{configProperties['uniview.device_id']}")
	private String device_id; // 设备Id

	@Value("#{configProperties['uniview.device_platform']}")
	private String device_platform; // 设备平台

	@Value("#{configProperties['uniview.workpuls_url']}")
	private String workpuls_url;

	@Value("#{configProperties['uniview.templateId']}")
	private String templateId;
	
	@Value("#{configProperties['uniview.CC.templateId']}")
	private String templateIdCC; //　抄送模板ID

	/**
	 * 推送workplus平台消息。
	 * 
	 * @param targetAlert
	 *            通知标题
	 * @param appMessage
	 *            接收对象
	 * @param bodyData
	 *            消息内容
	 */
	public void pushMessage(String targetAlert, WorkplusAppMessage appMessage, JSONArray bodyData) {
		try {
			WorkplusToken workplusToken = new WorkplusToken();
			workplusToken.setClient_id(client_id);
			workplusToken.setClient_secret(client_secret);
			workplusToken.setDevice_platform(device_platform);
			workplusToken.setDomain_id(domain_id);
			workplusToken.setGrant_type(grant_type);
			workplusToken.setScope(scope);
			workplusToken.setOrg_id(org_id);
			appMessage.setType("TEMPLATE");
			WorkplusService.sendWorkplusTemplateMessage(workpuls_url, workplusToken, targetAlert, appMessage,
					templateId, bodyData, device_platform);
		} catch (Exception e) {
			logger.error("消息推送异常", e);
		}
	}
	
	/**
	 * 抄送消息推送
	 * @param targetAlert
	 * @param appMessage
	 * @param bodyData
	 */
	public void pushMessageByCC(String targetAlert, WorkplusAppMessage appMessage, JSONArray bodyData) {
		try {
			WorkplusToken workplusToken = new WorkplusToken();
			workplusToken.setClient_id(client_id);
			workplusToken.setClient_secret(client_secret);
			workplusToken.setDevice_platform(device_platform);
			workplusToken.setDomain_id(domain_id);
			workplusToken.setGrant_type(grant_type);
			workplusToken.setScope(scope);
			workplusToken.setOrg_id(org_id);
			appMessage.setType("TEMPLATE");
			WorkplusService.sendWorkplusTemplateMessage(workpuls_url, workplusToken, targetAlert, appMessage,
					templateIdCC, bodyData, device_platform);
		} catch (Exception e) {
			logger.error("消息推送异常", e);
		}
	}

	/**
	 * 消息推送接口方法
	 * 
	 * @param targetAlert
	 * @param targetUrl
	 * @param content
	 * @param bodyData
	 */
	public void pushMessage(String targetAlert, String targetUrl, String content, JSONArray bodyData) {
		try {
			WorkplusAppMessage appMessage = new WorkplusAppMessage();
			WorkplusMessageBody body = new WorkplusMessageBody(content, targetUrl);
			
			appMessage.setBody(body);
			pushMessage(targetAlert, appMessage, bodyData);
		} catch (Exception e) {
			logger.error("消息推送异常", e);
		}
	}

	/**
	 * 消息推送接口方法
	 * 
	 * @param targetAlert
	 * @param targetUrl
	 * @param content
	 * @param orderMessage
	 */
	public void pushMessage(String targetAlert, String targetUrl, String content, OrderMessage orderMessage,
			List<String> userNames) {
		try {
			WorkplusAppMessage appMessage = new WorkplusAppMessage();
			WorkplusMessageBody body = new WorkplusMessageBody(content, targetUrl);
			appMessage.setBody(body);
			appMessage.setUserNames(userNames);

			JSONArray bodyData = new JSONArray();
			Class<?> userCla = (Class<?>) orderMessage.getClass();
			Field[] fs = userCla.getDeclaredFields();
			for (int i = 0; i < fs.length; i++) {
				Field f = fs[i];
				f.setAccessible(true);
				JSONObject obj = new JSONObject();
				if(f.get(orderMessage) != null){
					obj.put("key", f.getName());
					obj.put("value", f.get(orderMessage));
					bodyData.add(obj);
				}
			}

			pushMessage(targetAlert, appMessage, bodyData);
		} catch (Exception e) {
			logger.error("消息推送异常", e);
		}
	}
	
	
	public void pushMessageByCC(String targetAlert, String targetUrl, String content, Map<String, String> ccMap,
			List<String> userNames) {
		try {
			WorkplusAppMessage appMessage = new WorkplusAppMessage();
			WorkplusMessageBody body = new WorkplusMessageBody(content, targetUrl);
			appMessage.setBody(body);
			appMessage.setUserNames(userNames);

			JSONArray bodyData = new JSONArray();
			JSONObject obj = new JSONObject();
			obj.put("key", "app");
			obj.put("value", ccMap.get("app"));
			bodyData.add(obj);
			obj = new JSONObject();
			obj.put("key", "date");
			obj.put("value", ccMap.get("date"));
			bodyData.add(obj);
			obj = new JSONObject();
			obj.put("key", "creator");
			obj.put("value", ccMap.get("creator"));
			bodyData.add(obj);
			obj = new JSONObject();
			obj.put("key", "content");
			obj.put("value", ccMap.get("content"));
			bodyData.add(obj);
			obj = new JSONObject();
			obj.put("key", "url");
			obj.put("value", ccMap.get("url"));
			bodyData.add(obj);

			pushMessageByCC(targetAlert, appMessage, bodyData);
		} catch (Exception e) {
			logger.error("消息推送异常", e);
		}
	}
}
